﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;

namespace weikebuluo.Common
{
    public class BaiduLocation
    {
        public double gpsx, gpsy;
        public double baidux, baiduy;
        public bool ok = false;

        JavaScriptSerializer Jss = new JavaScriptSerializer();

        public BaiduLocation(double x, double y)
        {
            this.gpsx = x;
            this.gpsy = y;

            GetBaiduLocation();
        }

        #region 转换GPS的坐标为百度地图坐标
        public void GetBaiduLocation()
        {
            string url = string.Format("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x={0}&y={1}", this.gpsx, this.gpsy);

            Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(webRequestPost(url, "", ""));

            string x = respDic["x"].ToString();
            string y = respDic["y"].ToString();
            string error = respDic["error"].ToString();

            if (error == "0")
            {
                x = ConvertBase64(x);
                y = ConvertBase64(y);

                this.ok = true;
                this.baidux = double.Parse(x);
                this.baiduy = double.Parse(y);
            }
        }
        #endregion 转换GPS的坐标为百度地图坐标

        #region Post/Get提交调用抓取
        /// <summary>
        /// Post 提交调用抓取
        /// </summary>
        /// <param name="url">提交地址</param>
        /// <param name="param">参数</param>
        /// <returns>string</returns>
        public string webRequestPost(string url, string param, string type)
        {
            byte[] bt = System.Text.Encoding.UTF8.GetBytes(param);

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url + (url.IndexOf("?") > -1 ? "" : "?") + param);
            req.Method = "Post";
            req.Timeout = 120 * 1000;
            req.ContentType = "application/x-www-form-urlencoded;";
            req.ContentLength = bt.Length;

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(bt, 0, bt.Length);
                reqStream.Flush();
            }

            using (WebResponse res = req.GetResponse())
            {
                //在这里对接收到的页面内容进行处理 

                Stream resStream = res.GetResponseStream();

                StreamReader resStreamReader = new StreamReader(resStream, System.Text.Encoding.UTF8);

                string resLine;

                System.Text.StringBuilder resStringBuilder = new System.Text.StringBuilder();

                while ((resLine = resStreamReader.ReadLine()) != null)
                {
                    resStringBuilder.Append(resLine + System.Environment.NewLine);
                }

                resStream.Close();
                resStreamReader.Close();

                return resStringBuilder.ToString();
            }
        }
        #endregion Post/Get提交调用抓取

        #region Base64解码
        public string ConvertBase64(string inStr)
        {

            byte[] bt = Convert.FromBase64String(inStr);

            string result = System.Text.Encoding.UTF8.GetString(bt);

            return result;
        }
        #endregion Base64解码
    }
}
